WEBVTT

00:01.280 --> 00:01.890
All right.

00:01.890 --> 00:03.720
And the last video we created.

00:03.770 --> 00:06.200
The equal weighted portfolio.

00:06.360 --> 00:11.860
And in this video we are going to create many random portfolios consisting of our six stocks.

00:11.910 --> 00:20.250
So we want to have kind of a cloud here with random portfolios and random means so that we have randomly

00:20.250 --> 00:21.220
generated.

00:21.240 --> 00:28.320
The weights of the constituents and the only constraint or assumption is that each single way is between

00:28.320 --> 00:29.710
0 and 1.

00:29.760 --> 00:37.230
So there are no short positions and of course the weights of all six stocks have to sum up to one.

00:37.230 --> 00:44.730
So let's start here and we still have defined our user defined function annualized the risk and return

00:45.180 --> 00:53.410
where we can transform a return data frame into a summary data frame with the annualized risk and return.

00:53.490 --> 00:55.310
And so let's start here.

00:55.320 --> 01:03.820
We import panda's name pi and met plop lip and also seaborne and again we are importing and working

01:03.820 --> 01:08.390
with our stocks the portfolio stocks data frame.

01:08.590 --> 01:11.340
So this is nothing new here.

01:11.710 --> 01:17.110
And then in the next step we create actually the returns data frame with the percentage change method

01:19.590 --> 01:25.890
so here we have our returns data frame Staley returns and bypassing our returns data frame to our user

01:25.890 --> 01:28.500
defined function annualized risk and return.

01:28.650 --> 01:35.840
We can simply create our summary data frame so let's have a look so here we have the annualized return

01:35.870 --> 01:41.850
and risk for a six stocks and now we want to create random portfolios.

01:42.030 --> 01:45.780
And first of all we have to define the number of constituents.

01:46.230 --> 01:53.020
So here in our example these six stocks and we save six to the variable number of assets so we have

01:53.120 --> 01:53.960
six.

01:54.390 --> 01:59.710
And then we define that in our first simulation we want to simulate 10 portfolios.

01:59.820 --> 02:10.030
So a number of portfolios of random portfolios is 10 so we have in total 10 random portfolios and in

02:10.030 --> 02:13.090
each portfolios we have our six stocks.

02:13.090 --> 02:20.980
So in total we have 60 a random weights and we can actually create random numbers with the name pi dot

02:21.100 --> 02:22.420
random module.

02:23.140 --> 02:29.820
So if we apply here the random module and the free selected the method random.

02:29.860 --> 02:30.820
So let's have a look here.

02:31.810 --> 02:40.540
So the random method returns a Random float in the half open interval between 0 and 1 and 2 we find

02:40.540 --> 02:43.980
that we want to have a 60 a random numbers.

02:44.080 --> 02:45.040
So let's have a look here

02:47.920 --> 02:52.070
so here we have 60 random floats between 0 and 1.

02:52.300 --> 03:01.750
And as we have 10 portfolios with each uh six stocks we can also then reshape here or a nun pie array

03:02.020 --> 03:09.250
to 10 rows which are the portfolios and 6 columns and we can do this with the reshape method.

03:10.900 --> 03:12.970
So here we have an array with 10 rows.

03:12.990 --> 03:20.510
So one two three and 10 I was 10 rows and in each row we have uh six elements.

03:20.530 --> 03:21.660
So we have six columns.

03:21.680 --> 03:25.960
So the first second third fourth fifth and sixth.

03:25.960 --> 03:27.740
So these are our random numbers.

03:27.760 --> 03:35.420
And the fear Rita run here the cell and recreate our random numbers then we should get different numbers.

03:35.440 --> 03:42.030
So here the very first numbers 0 point 5 2 and if you rerun here the cell we get 0 point 7 3.

03:42.760 --> 03:49.960
And if you work with random numbers that might be desirable to have some kind of reproducibility and

03:50.080 --> 03:53.410
we can do this by setting a random seed.

03:53.530 --> 04:00.310
So we are using here the seed method and we pass an integer as well for example 1 2 3 and then in the

04:00.310 --> 04:02.740
next step when we are creating random numbers.

04:02.830 --> 04:09.610
So whenever we have a set of the same seed then we get the identical random numbers.

04:09.610 --> 04:12.280
So to say upside or random numbers.

04:12.280 --> 04:16.240
So first of all we set CAC to off one two three.

04:16.810 --> 04:24.830
And then again we're creating here a num pi matrix with 10 rows and six columns.

04:24.880 --> 04:29.830
So we are using the random method and we pass an always on number of assets.

04:29.830 --> 04:31.600
Times number of portfolios.

04:31.610 --> 04:33.570
So this is six times ten.

04:33.790 --> 04:41.920
And then we reshape to 10 rows and six columns and then we actually saving the NUM pi array and the

04:41.920 --> 04:42.850
rabble matrix.

04:42.850 --> 04:46.820
So let's do this and let's have a look.

04:46.840 --> 04:49.250
So this our 60 random floats.

04:49.390 --> 04:55.720
And if you rerun the code here we should exactly get the same random numbers as we set a seed.

04:55.720 --> 04:57.570
So let's try this out.

04:57.580 --> 05:05.580
So here the very first numbers 0 point 6 9 and yet still 0 point 6 9.

05:05.580 --> 05:11.430
So here we have our 10 portfolios with uh each six weights.

05:11.650 --> 05:17.130
And we can easily see here that debates that do not sum up to a one.

05:17.130 --> 05:19.420
So here we have the first portfolio.

05:19.710 --> 05:23.530
And here we have 69 percent 71 percent.

05:23.580 --> 05:29.680
So we are far away from a sum of weights of one.

05:29.700 --> 05:38.100
However we can simply normalize the weights here and get the weights that sum up to 1 and the first

05:38.100 --> 05:45.450
of all that's created the sum of weights for each portfolio and we can do this by applying the sum method

05:45.480 --> 05:48.510
and pass one to the axis pair meter.

05:48.600 --> 05:56.280
So we want to have the sums for all rows and we actually pass true to the keep them parameter to have

05:56.280 --> 05:57.550
an umpire array.

05:57.570 --> 06:03.350
Still with 10 rows and one column so let's have a look.

06:03.400 --> 06:06.910
So this the sums for the rose so far the first row.

06:06.910 --> 06:12.910
We have a sum of two point nine and get actually rates that sum up to one.

06:12.910 --> 06:20.760
We simply have to divide each element here in one portfolio on one row by the sum of the row.

06:20.770 --> 06:24.670
So for example the sum of the first row is two point nine.

06:24.670 --> 06:30.640
And we have to divide each single element in our first row by two point nine.

06:30.670 --> 06:37.430
And by doing so we are normalizing here our values and the values that then should sum up to 1.

06:37.450 --> 06:38.780
So let's do this year.

06:38.800 --> 06:46.870
So we're dividing our matrix by the sum of the rows of the matrix and then we are saving actually the

06:46.870 --> 06:52.440
resulting matrix with weights and all rows that sum up to 1 in the variable rates.

06:52.450 --> 07:00.020
So let's do this and let's have a look so here we have again our first portfolio.

07:00.020 --> 07:03.610
And here we have a point two three 0 point too far.

07:03.620 --> 07:06.950
So this pretty looks good actually.

07:06.950 --> 07:13.010
And we can also prove that in each portfolio the weights sum up to 1.

07:13.460 --> 07:23.250
So again we use here the sum method and we can see here that needs a single row for each single portfolio.

07:23.250 --> 07:26.330
We have a total sum up a weight of 11.

07:26.340 --> 07:33.570
So this is exactly what we need and now we can calculate the re weighted average daily return for each

07:33.580 --> 07:38.920
random portfolio and for each time stamp with the top method.

07:38.920 --> 07:45.820
So we use our returns data frame and we pass Yeah our transposed weights num pi array to the top method.

07:46.390 --> 07:54.040
And by doing so we're creating a data frame with portfolio returns for our 10 portfolios.

07:54.040 --> 07:58.540
And actually we save the portfolio returns on the variable part red.

07:58.660 --> 08:05.760
So let's do this and let's have a look so here he CFS columns our 10 random portfolios starting from

08:05.760 --> 08:09.800
0 to 9 and still we have each timestamp.

08:10.020 --> 08:18.660
And here we have actually the daily returns for all of our random portfolios and now in the next step

08:18.660 --> 08:24.980
we can also create the summary data frame of our random portfolios by passing here.

08:24.990 --> 08:30.650
The portfolio returns to the annualized the risk return user defined function.

08:30.660 --> 08:39.280
So let's do this and let's have a look so here we have now on the left hand side as the index our random

08:39.280 --> 08:47.530
portfolios from 0 to 9 and we have actually the annualized the returns and the annualized the risk for

08:47.530 --> 08:51.100
each and every random portfolio here.

08:51.190 --> 08:55.820
And finally we can also plot our random portfolios and create a set up plot.

08:56.390 --> 09:03.400
So first of all we create a scatter plot for our Random portfolios with actually the risk on the x axis

09:03.400 --> 09:06.010
and the return on the y axis.

09:06.220 --> 09:13.240
And we also plotted our six stocks with the risk on the x axis and the return on the y axis.

09:13.480 --> 09:16.680
And actually our random portfolios have a size of 20.

09:16.690 --> 09:27.230
And a red color and our stocks have a higher size of 50 year black colour and a marker of the year.

09:27.250 --> 09:30.070
So Diamond so let's have a look here

09:36.280 --> 09:41.620
so here you have now 10 random portfolios and this looks quite nice.

09:41.650 --> 09:48.970
However if you want to perform a deeper analysis we need a lot of more random portfolios and we can

09:48.970 --> 09:52.690
do so by simply increasing the number of portfolios.

09:52.720 --> 09:54.280
Let's go up again here

09:58.180 --> 10:01.450
so here we have the numbers of portfolios.

10:01.450 --> 10:04.810
And for example we could simulate and set of 10.

10:04.810 --> 10:08.640
We could simulate 100 a thousand random portfolios.

10:08.650 --> 10:09.790
So let's throw this here.

10:13.750 --> 10:17.560
And what we simply have to do now we have to rerun our code.

10:17.560 --> 10:21.020
So starting with this line here.

10:21.070 --> 10:27.280
So we are creating 100000 portfolios each consisting of our six stocks with random weights

10:31.630 --> 10:41.000
so let's again normalize the weights to have a sum of 1 and let's check it here so for each portfolio

10:41.030 --> 10:47.710
we FBA some of weights of 1 and let's create the daily returns for each and every random portfolio

10:50.120 --> 10:59.390
so here we have our random portfolios starting with the very first one and ending at portfolio one hundred

10:59.570 --> 11:07.730
thousand and then we also create the annualized risk and return for each and every portfolio by passing

11:07.730 --> 11:12.090
year the portfolio return to our user defined function.

11:12.110 --> 11:20.770
So this is the most time consuming operation here and it could take a while so let's simply wait here

11:25.130 --> 11:31.340
so we are finished and here we have the annualized risk and return for all 100000 portfolios

11:34.210 --> 11:35.930
so let's have a look down here.

11:36.070 --> 11:45.700
So it's here until we have fear of 100000 portfolios and the then we can create here our cloud of portfolios.

11:45.700 --> 11:46.570
So let's say this

11:49.860 --> 11:57.500
and if we can see and read our 100000 portfolios in black here we have the risk return profile of our

11:57.500 --> 12:00.110
six constituent stocks.

12:00.170 --> 12:01.540
So this looks pretty cool.

12:01.550 --> 12:08.810
And in the next video we are going to analyze our 100000 portfolios so hope to see there by.
